package service;

import java.util.Properties;

import com.agentfactory.afapl2.debugger.AFAPL2StateManagerFactory;
import com.agentfactory.afapl2.debugger.inspector.AFAPL2InspectorFactory;
import com.agentfactory.afapl2.interpreter.AFAPL2ArchitectureFactory;
import com.agentfactory.platform.core.IAgent;
import com.agentfactory.platform.core.NoSuchServiceException;
import com.agentfactory.platform.impl.DefaultAgentPlatform;
import com.agentfactory.platform.impl.PlatformServiceManager;
import com.agentfactory.platform.impl.RoundRobinTimeSliceFixedScheduler;
import com.agentfactory.service.ams.AgentManagementService;
import com.agentfactory.visualiser.Debugger;

public class Main {
	
	public static void main(String[] args){
		// Create a new agent platform with a basic name and domain
        DefaultAgentPlatform platform = new DefaultAgentPlatform();
        platform.setName("test");
        platform.setDomain("ucd.ie");

        // Install a scheduling algorithm for executing the agents
        platform.setScheduler(new RoundRobinTimeSliceFixedScheduler());
        
        // Install and register the AFAPL2 Architecture Factory:
        // This enables support for instantiating AFAPL2 agents (i.e. agents
        // whose source code is identified by a .agent extension)
        AFAPL2ArchitectureFactory factory = new AFAPL2ArchitectureFactory();
        Properties props = new Properties();
        props.setProperty("TIMESLICE", "1000");
        factory.configure(props);
        platform.getArchitectureService().registerArchitectureFactory(factory);
        
        PlatformServiceManager manager = ((PlatformServiceManager) platform.getPlatformServiceManager());
        try {
            manager.addService(SensorNetPlatformService.class, "sensornet");
        } catch (NoSuchServiceException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Install and start the Agent Factory Debugger
        Debugger debugger = new Debugger(platform);
        debugger.registerInspectorFactory(new AFAPL2InspectorFactory());
        debugger.registerStateManagerFactory(new AFAPL2StateManagerFactory());
        debugger.start();
        
		
		AgentManagementService ams = (AgentManagementService) platform.getPlatformServiceManager().getServiceByName(AgentManagementService.NAME);
		try {
			IAgent agent1 = ams.createAgent("sensie1", "afapl/Test.agent");
			IAgent agent2 = ams.createAgent("sensie2", "afapl/Test.agent");
			IAgent agent3 = ams.createAgent("sensie3", "afapl/Test.agent");
			IAgent agent4 = ams.createAgent("sensie4", "afapl/Test.agent");
			IAgent agent5 = ams.createAgent("sensie5", "afapl/Test.agent");
			IAgent agent6 = ams.createAgent("sensie6", "afapl/Test.agent");
			IAgent agent7 = ams.createAgent("sensie7", "afapl/Test.agent");
			IAgent agent8 = ams.createAgent("sensie8", "afapl/Test.agent");
		} catch (Exception ex) {
		}

	}

}
